package com.controller;

import com.bean.Collect;
import com.bean.User;
import com.google.gson.Gson;
import com.service.CollectService;
import com.service.FlowerService;
import com.service.impl.CollectServiceImpl;
import com.service.impl.FlowerServiceImpl;
import com.sun.org.apache.xpath.internal.operations.Bool;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

public class CollectServlet extends BaseServlet {
    private CollectService collectService=new CollectServiceImpl();
    private FlowerService flowerService=new FlowerServiceImpl();

    /**
     * 添加到我的收藏夹
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取当前用户编号
        User user= (User) req.getSession().getAttribute("user");
        if (user == null) {
            req.getRequestDispatcher("page/loginUser/login.jsp").forward(req,resp);
        }
        String userid=user.getUserid();
        //获取鲜花产品编号
        String id=req.getParameter("id");
        Integer flowerid=Integer.parseInt(id);
        //collectService.queryId(flowerid);

       //生成收藏编号唯一性
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String date2=sdf.format(new Date());
        String first="";
        Random random=new Random();
        for(int i=0;i<3;i++){
            first+=random.nextInt(10);
        }
        String collectid =date2+first;
        //获取当前时间
        Date createtime=new Date();
        System.out.println(createtime);


        collectService.add(collectid,userid,flowerid,createtime);

        resp.sendRedirect(req.getHeader("Referer"));
      //  req.getRequestDispatcher("/page/user/index.jsp").forward(req,resp);
    }

    /**
     * 查看指定用户收藏夹
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void queryByUserid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user= (User) req.getSession().getAttribute("user");
        String userid=user.getUserid();
       /* List<Collect> collects=null;
        collects=collectService.queryByUser(userid);*/
       List<Map<String,Object>> collects=collectService.listqueryByUser(userid);
       System.out.println(collects);
        req.setAttribute("collects",collects);
         req.getRequestDispatcher("/page/user/myCollect.jsp").forward(req,resp);
    }

    /**
     * 移出收藏夹
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String collectid=req.getParameter("collectid");
        collectService.deleteByCollectid(collectid);
        User user= (User) req.getSession().getAttribute("user");
        String userid=user.getUserid();
        List<Map<String,Object>> collects=collectService.listqueryByUser(userid);
        System.out.println(collects);
        req.setAttribute("collects",collects);
        req.getRequestDispatcher("/page/user/myCollect.jsp").forward(req,resp);
    }

    /**
     * 清空收藏夹
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        User user= (User) req.getSession().getAttribute("user");
        if(user==null ){
            req.getRequestDispatcher("/page/loginUser/login.jsp").forward(req,resp);
        }
        collectService.deleteByUserid(user.getUserid());
        req.getRequestDispatcher("/page/user/myCollect.jsp").forward(req,resp);
    }

    /**
     * 查看收藏夹是否已含有id
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void ajaxQuery(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user= (User) req.getSession().getAttribute("user");
        String userid=user.getUserid();
        String id=req.getParameter("flowerid");
        Integer flowerid=Integer.parseInt(id);
         boolean existsId=collectService.ajaxQueryId(userid,flowerid);
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("existsId",existsId);
        System.out.println(resultMap);
        Gson gson=new Gson();
        String json=gson.toJson(resultMap);
        System.out.println(json);
        resp.getWriter().write(json);
    }
}
